﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Dapper.Contrib.Extensions;
using pinduoduo.Model;
using pinduoduo.Util;
using Tk.Soc.Share.Common.Helpers;

namespace pinduoduo.Service
{
    public class PddTokenService
    {
        public IEnumerable<PddToken> GetAllToken(bool isDecodeAESToken = true)
        {
            var tokens = DataBaseHelper.SqLiteDBCon.GetAll<PddToken>();
            foreach (var item in tokens)
            {
                try
                {
                    if (isDecodeAESToken) {
                        item.PDDAccessToken = AESHelper.DecodeAES(item.PDDAccessToken).Replace("\0", "").Replace("\r\n", "");
                    }
                }
                catch (System.Exception e)
                {
                    LogHelper.Logger.Error("解析Token失败", e);
                    item.PDDAccessToken = "";
                }

            }
            return tokens;
        }

        public async Task<PddToken> CheckePddTokenStatus(PddToken pddToken)
        {
            string errorMsg;
            var status = G_Api.GetPddTokenStatus(pddToken.pdd_user_id,out errorMsg);

            if (status == -1)
            {
                pddToken.status = "过期";
            }
            else if (status == 1)
            {
                pddToken.status = "未知";
            }
            else if (status == 0) 
            {
                var pddUserInfo = await PddApi.GetPddMyInfo(pddToken.pdd_user_id, pddToken.PDDAccessToken);

                if (pddUserInfo.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    pddToken.status = "正常";
                }
                else if (pddUserInfo.Content.Contains("error_code"))
                {
                    pddToken.status = "过期";
                }
                else if (pddUserInfo.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    pddToken.status = "未知";
                }

            }

            UpdateToken(pddToken, true);

            return pddToken;

        }

        /// <summary>
        /// 不包含主号
        /// </summary>
        /// <param name="isDecodeAESToken"></param>
        /// <returns></returns>
        public IEnumerable<PddToken> GetShuaDanToken(bool isDecodeAESToken = true)
        {

            var tokens = GetAllToken(isDecodeAESToken);
            tokens= tokens.Where(x => x.account_type != 1).ToList();
            return tokens;
        }


        public long InsertToken(PddToken pddToken, bool isEncodeAESToken = false)
        {
            try
            {
                PddToken pddTokenNew = DeepCopyExtensions<PddToken>.DeepCopy(pddToken);
                if (isEncodeAESToken)
                {
                    pddTokenNew.PDDAccessToken = AESHelper.EncodeAES(pddToken.PDDAccessToken);
                }

                return DataBaseHelper.SqLiteDBCon.Insert(pddTokenNew);
            }
            catch (System.Exception e)
            {
                LogHelper.Logger.Error("加密Token失败", e);
            }

            return 0;
        }

        public bool UpdateToken(PddToken pddToken, bool isEncodeAESToken = false)
        {

            try
            {
                PddToken pddTokenNew = DeepCopyExtensions<PddToken>.DeepCopy(pddToken);
                if (isEncodeAESToken)
                {
                    pddTokenNew.PDDAccessToken = AESHelper.EncodeAES(pddToken.PDDAccessToken);
                }
                return DataBaseHelper.SqLiteDBCon.Update<PddToken>(pddTokenNew);
            }
            catch (System.Exception e)
            {
                LogHelper.Logger.Error("加密Token失败", e);
            }

            return false;
        }

        public bool DelToken(PddToken pddToken)
        {
             return DataBaseHelper.SqLiteDBCon.Delete(pddToken);
        }

        public bool DelAllToken()
        {
            return DataBaseHelper.SqLiteDBCon.DeleteAll<PddToken>();
        }
    }
}
